home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / dev / gcc / libnix.lha / gnu / lib / libnix / sources.lha / nix / time / strftime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-16  |  3.4 KB  |  133 lines

  1. #include <time.h>
  2. #include <locale.h>
  3. #include <libraries/locale.h>
  4. #ifdef __GNUC__
  5. #include <inline/locale.h>
  6. #endif
  7.  
  8. #define ADDS(st)  tmp=strftime(s,maxsize-size,(st),timeptr);break;              
  9.  
  10. #define ADDN(a,b) tmp=strfnumb(s,maxsize-size,(a),(b));break;
  11.  
  12. #define STOR(c)   if(++size<=maxsize)*s++=(c);
  13.  
  14. #define STR(a) \
  15. (__localevec[LC_TIME-1]==NULL?strings[(a)-1]:GetLocaleStr(__localevec[LC_TIME-1],(a)))
  16.  
  17. extern struct Locale *__localevec[];
  18.  
  19. /* All calendar strings */
  20. static const unsigned char *strings[]=
  21. { "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",
  22.   "Sun","Mon","Tue","Wed","Thu","Fri","Sat",
  23.   "January","February","March","April","May","June",
  24.   "July","August","September","October","November","December",
  25.   "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",
  26.   "","","AM","PM" };
  27.  
  28. static size_t strfnumb(char *s,size_t maxsize,signed int places,size_t value)
  29. { size_t size=0;
  30.   if(places>1)
  31.     size=strfnumb(s,maxsize,places-1,value/10);
  32.   else if(value>=10)
  33.     size=strfnumb(s,maxsize,places+1,value/10);
  34.   else
  35.     while(places++<-1)
  36.       STOR(' ');
  37.   if(++size<=maxsize)
  38.     s[size-1]=(value%10+'0');
  39.   return size;
  40. }
  41.       
  42. size_t strftime(char *s,size_t maxsize,const char *format,const struct tm *timeptr)
  43. { size_t size=0,tmp;
  44.   while(*format)
  45.   { if(*format=='%')
  46.     { tmp=0;
  47.       switch(*++format)
  48.       { case 'a':
  49.       ADDS(STR(ABDAY_1+timeptr->tm_wday));
  50.         case 'b':
  51.         case 'h':
  52.           ADDS(STR(ABMON_1+timeptr->tm_mon));
  53.         case 'c':
  54.           ADDS("%m/%d/%y");
  55.         case 'd':
  56.           ADDN(2,timeptr->tm_mday);
  57.         case 'e':
  58.           ADDN(-2,timeptr->tm_mday);
  59.         case 'j':
  60.           ADDN(3,timeptr->tm_yday+1);
  61.         case 'k':
  62.           ADDN(-2,timeptr->tm_hour);
  63.         case 'l':
  64.           ADDN(-2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
  65.         case 'm':
  66.           ADDN(2,timeptr->tm_mon+1);
  67.         case 'p':
  68.           ADDS(STR(AM_STR+(timeptr->tm_hour>=12)));
  69.         case 'r':
  70.           ADDS("%I:%M:%S %p");
  71.         case 'w':
  72.           ADDN(1,timeptr->tm_wday);
  73.         case 'x':
  74.           ADDS("%m/%d/%y %H:%M:%S");
  75.         case 'y':
  76.           ADDN(2,timeptr->tm_year%100);
  77.         case 'A':
  78.           ADDS(STR(DAY_1+timeptr->tm_wday));
  79.         case 'B':
  80.           ADDS(STR(MON_1+timeptr->tm_mon));
  81.         case 'C':
  82.           ADDS("%a %b %e %H:%M:%S %Y");
  83.         case 'D':
  84.           ADDS("%m/%d/%y");
  85.         case 'H':
  86.           ADDN(2,timeptr->tm_hour);
  87.         case 'I':
  88.           ADDN(2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
  89.         case 'M':
  90.           ADDN(2,timeptr->tm_min);
  91.         case 'R':
  92.           ADDS("%H:%M");
  93.         case 'S':
  94.           ADDN(2,timeptr->tm_sec);
  95.         case 'T':
  96.         case 'X':
  97.           ADDS("%H:%M:%S");
  98.         case 'U':
  99.           ADDN(2,(timeptr->tm_yday+7-timeptr->tm_wday)/7);
  100.         case 'W':
  101.           ADDN(2,(timeptr->tm_yday+7-(6+timeptr->tm_wday)%7)/7);
  102.         case 'Y':
  103.           ADDN(4,timeptr->tm_year+1900);
  104.         case 't':
  105.           STOR('\t');
  106.           break;
  107.         case 'n':
  108.           STOR('\n');
  109.           break;
  110.         case '%':
  111.           STOR('%');
  112.           break;
  113.         case '\0':
  114.           format--;
  115.           break;
  116.       }
  117.       size+=tmp;
  118.       s+=tmp;
  119.     }
  120.     else
  121.       STOR(*format);
  122.     format++;
  123.   }
  124.   STOR('\0');
  125.   if(size>maxsize)
  126.   { s-=size;
  127.     if(maxsize) /* Don't know if this is necessary, therefore it's here ;-) */
  128.       s[maxsize-1]='\0';
  129.     size=1;
  130.   }
  131.   return size-1;
  132. }
  133.